package com.sunrise.gateway.commons;

import org.apache.shiro.session.Session;
import org.apache.shiro.session.mgt.SimpleSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author wm
 * @discription
 * @created 2020-10-13 10:31
 */
public abstract class MySessionUtil {
    private final static Logger logger = LoggerFactory.getLogger(MySessionUtil.class);

    public static boolean isSessionValid(Session session) {
        if (null == session) {
            return false;
        }

        try {
            if (session instanceof SimpleSession) {
                return ((SimpleSession) session).isValid();
            } else {
                // 如果session非法, 触发调用会抛出异常
                session.getTimeout();
            }
        } catch (Exception e) {
            logger.debug("", e);
            return false;
        }

        return true;
    }
}
